package org.zzh.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zzh.springboot.dao.UserMapper;
import org.zzh.springboot.entity.User;
import org.zzh.springboot.service.UserService;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String name, String password) {
        return userMapper.login(name, password);
    }


//    @Override
//    public List<User> getUserList() {
//        return userMapper.selectAll();
//    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int  deleteUserById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public List<User> getUserList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectAll();
    }

//    @Override
//    public List<User> searchUsers(String keyword, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        return userMapper.searchUsersByName(keyword);
//    }

    @Override
    @Transactional
    public int deleteAllByUsername(String name) {
        return userMapper.deleteByUsername(name);
    }

    @Override
    public User findUserByName(String name) {
        User user = new User();
        user.setName(name);
        return userMapper.selectOne(user);
    }

    @Override
    public List<User> searchUsers(String name, String sex, String address, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.searchUsersByConditions(name, sex, address);
    }

}
